package example.com.terry.servlet;


import example.com.terry.utils.JDBCUtils;
import example.com.terry.utils.StringUtils;

import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Logger;

public class LoginServlet implements Servlet {
    private static Logger log = Logger.getLogger("LoginServlet");

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //servlet 初始化执行方法 可以用来判断登陆状态 如果不是登陆状态 直接销毁
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
        // 业务逻辑方法
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        if (StringUtils.isBlank(username) && StringUtils.isBlank(password)) {
            //todo 账号密码为空 直接返回
        } else {
            //todo 开始判断登陆逻辑
            String sql = "select * from tb_user where username = '" + username + "' and  password = '" + password + "'";
            ResultSet resultSet = JDBCUtils.initQuery(sql);
            try {
                if (resultSet.next()) {
                    //todo 获取数据库内用户信息
                    String dbUsername = resultSet.getString("username");

                    String dbPassword = resultSet.getString("password");
                    if (username.equals(dbUsername) && password.equals(dbPassword)) {
                        //todo 如果当前登陆用户存在数据库中
                        Cookie cookie = new Cookie("username", username);
                        // 设置cookie 一小时有效期
                        cookie.setMaxAge(1000 * 60 * 60);
                        HttpServletRequest req1 = (HttpServletRequest) req;
                        HttpServletResponse resp1 = (HttpServletResponse) resp;
                        resp1.addCookie(cookie);
                        // 登陆成功初始化页面 请求接口 可以改成请求页面
                        req.getRequestDispatcher("/WEB-INF/jsp/page.jsp").forward(req1, resp1);
                        //req.setAttribute("type", "0");
                        // req.getRequestDispatcher("/jump").forward(req1, resp1);
                    } else {
      //                  log.info("----------密码错误----------");
                        resp.getWriter().println("密码错误");
                    }
                } else {
                    log.info("----------账号不存在----------");
                    resp.getWriter().println("账号不存在");
                }
            } catch (SQLException e) {
                log.info("----------登陆方法解析用户数据异常----------");
            }
        }
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {


    }
/*    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // TODO Auto-generated method stub
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");
        String username = req.getParameter("username");
        String userpass = req.getParameter("password");
        super.doPost(req, resp);
    }*/
}
